package com.test.resumeanalysis.util

import android.annotation.SuppressLint
import android.content.Context
import android.net.Uri
import android.provider.OpenableColumns
import android.util.Base64
import com.test.resumeanalysis.data.model.ResumeRequestBean

import java.io.*

import java.io.File
import java.io.FileInputStream
import java.io.IOException

object FileUtil {
    suspend fun createTxtFile(filePath: String, content: String) {
        val file = File(filePath)
        try {
            val fileWriter = FileWriter(file)
            fileWriter.write(content)
            fileWriter.close()

        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    suspend fun jpgToBase64(filePath: String):String{
        val file = File(filePath)
        val bytes = ByteArray(file.length().toInt())
        try {
            val inputStream = FileInputStream(file)
            inputStream.read(bytes)
            inputStream.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        var base64 = "data:image/jpeg;base64,"+Base64.encodeToString(bytes, Base64.DEFAULT)

        base64=base64.replace("\\n","")
        return base64.trim().replace("\\s+".toRegex(), "")
    }

    suspend fun filetoBase64(filePath: String): ResumeRequestBean {
        val file = File(filePath)
        LogUtil.e(filePath)
        val bytes = ByteArray(file.length().toInt())
        try {
            val inputStream = FileInputStream(file)
            inputStream.read(bytes)
            inputStream.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        var base64 =
//            "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,"+
                    Base64.encodeToString(
                bytes,
                Base64.DEFAULT
            )

        base64 = base64.replace("\\n", "")

        return ResumeRequestBean(file.name, base64.trim().replace("\\s+".toRegex(), ""))
    }

    @SuppressLint("Range")
    fun getFileRealPath(context: Context, uri: Uri?): String? {
        var realPath: String? = null
        uri?.let { uri ->
            val contentResolver = context.contentResolver
            val cursor = contentResolver.query(uri, null, null, null, null)
            cursor?.use { cursor ->
                if (cursor.moveToFirst()) {
                    val displayName =
                        cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
                    val inputStream = contentResolver.openFileDescriptor(uri, "r")?.fileDescriptor?.let {
                        FileInputStream(it)
                    }
                    val file = context.cacheDir.resolve(displayName)
                    inputStream?.use { input ->
                        FileOutputStream(file).use { output ->
                            val buffer = ByteArray(4 * 1024) // or other buffer size
                            var read: Int
                            while (input.read(buffer).also { read = it } != -1) {
                                output.write(buffer, 0, read)
                            }
                            output.flush()
                        }
                    }
                    realPath = file.absolutePath
                }
            }
        }
        return realPath
    }



}